// pages/card/carddetl/carddetl.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    active: "1",
    cardId: null,
    cardName: null,
    qryUserId: null,
    recordList: [],
    recordMap: new Map(),
  },
  onScroll(event) {
    let isScrollEnd = false;
    if (event.detail.scrollTop > 10) {
      isScrollEnd = true;
    }
    if (isScrollEnd != this.data.isScrollEnd) {
      this.setData({ isScrollEnd: isScrollEnd })
    }
    this.setData({ scrollTop: event.detail.scrollTop });
  },
  // Tab页变动
  onChangeTab(event) {
    const recordList = this.data.recordMap.get(event.detail.name);
    this.setData({ recordList: recordList || [] })
    this.setData({ active: event.detail.name })
    this.getScrollHeight();
  },
  qryCardRecordList() {
    const param = {
      cardId: this.data.cardId,
      qryUserId: this.data.qryUserId
    }
    getApp().callContainer('/api/user/card/listRecord', param, (res) => {
      if (res.data.data.recordList) {
        // 转化为recordMap，key为type value为实体本身
        const recordMap = new Map();
        for (const record of res.data.data.recordList) {
          const key = record.type + "";
          if (recordMap.has(key)) {
            recordMap.get(key).push(record);
          } else {
            recordMap.set(key, [record]);
          }
        }
        const recordList = recordMap.get(this.data.active);
        this.setData({ recordList: recordList, recordMap: recordMap });
      }
    });
  },
  getScrollHeight() {
    // 获取内容区域的高度
    wx.createSelectorQuery().selectViewport().scrollOffset((res) => {
      this.setData({ scrollHeight: res.scrollHeight + 'px' });
    }).exec()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      cardId: options.cardId,
      cardName: options.cardName,
      qryUserId: options.userId
    });
    this.qryCardRecordList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})